#!/bin/sh

# I'm sure replicating much of debian/rules is entirely wrong, but I'm going to
# do it anyway...

if ! dpkg-checkbuilddeps; then
	sudo apt-get -y build-dep lingmo-installer
fi

if [ ! -e d-i/source ]; then
	make -C d-i update
	make -C d-i build
fi
[ -e src/Makefile ] || ./configure
make -C src

rebuild=false
if [ ! -e d-i/templates ]; then
	# For debian-installer-utils
	DEB_HOST_ARCH_OS=$(dpkg-architecture -qDEB_HOST_ARCH_OS) \
	fakeroot make -C d-i install
	rebuild=:
fi
# For loading the console-setup plugin.  This is needed for at least testing
# loading of plugins.
[ -e lingmo-installer/keyboard_names.py ] || ./debian/rules lingmo-installer/keyboard_names.py

[ -e tests/debconf-stamp ] || rebuild=:
if ! $rebuild && [ -e tests/debconfdb ]; then
	for x in d-i/templates debian/lingmo-installer.templates \
		debian/lingmo-installer.templates-imported; do
		if [ $x -nt tests/debconf-stamp ]; then
			rebuild=:
			break
		fi
	done
else
	rebuild=:
fi
$rebuild && (cat d-i/templates; echo;
	po2debconf debian/lingmo-installer.templates; echo;
	po2debconf debian/lingmo-installer.templates-imported \
		--podir=debian/imported-po) > tests/debconfdb
    touch tests/debconf-stamp

export DEBCONF_SYSTEMRC=tests/debconf.conf
$rebuild && debconf-loadtemplate lingmo-installer tests/debconfdb

rm -rf tests/partman-tree
for x in d-i/source/part*.udeb; do dpkg -x $x tests/partman-tree; done
